package com.foreverwin.jzymes.common.service.impl;

import com.foreverwin.jzymes.common.domain.CarrierBind;
import com.foreverwin.jzymes.common.domain.NcData;
import com.foreverwin.jzymes.common.enums.DispositionEnum;
import com.foreverwin.jzymes.common.mapper.NcDataAdviceMapper;
import com.foreverwin.jzymes.common.mapper.NcDataMapper;
import com.foreverwin.jzymes.common.service.ICarrierBindAdviceService;
import com.foreverwin.jzymes.common.service.INCDataAdviceService;
import com.foreverwin.jzymes.common.service.IResourceAdviceService;
import com.foreverwin.jzymes.common.util.BOHandleUtil;
import com.foreverwin.jzymes.common.util.CommonMethods;
import com.foreverwin.jzymes.framework.util.I18nUtil;
import com.foreverwin.jzymes.production.sfc.domain.SFC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class NCDataAdviceServiceImpl implements INCDataAdviceService {

    @Autowired
    IResourceAdviceService resourceAdviceService;
    @Autowired
    ICarrierBindAdviceService carrierBindAdviceService;
    @Autowired
    NcDataMapper ncDataMapper;
    @Autowired
    NcDataAdviceMapper ncDataAdviceMapper;

    /**
     * 保存不良数据
     *
     * @param sfc
     * @param resource
     * @param carrier
     * @param dateCode
     * @param ncCode
     * @param ncQty
     * @param dispositionEnum
     * @param date
     */
    public void saveNcData(SFC sfc, String carrier, String dateCode, String resource, String ncCode, Long ncQty, DispositionEnum dispositionEnum, Date date ){
        String operation = resourceAdviceService.findOperationByResource( sfc.getSite(), resource );
        saveNcData( sfc, carrier, dateCode, resource, operation, ncCode, ncQty, dispositionEnum, date );
    }

    /**
     * 保存不良数据
     *
     * @param sfc
     * @param carrier
     * @param dateCode
     * @param resource
     * @param operation
     * @param ncCode
     * @param ncQty
     * @param dispositionEnum
     * @param date
     */
    public void saveNcData(SFC sfc, String carrier, String dateCode, String resource, String operation, String ncCode, Long ncQty, DispositionEnum dispositionEnum, Date date ){
        NcData ncData = new NcData();
        ncData.setSite( sfc.getSite() );
        ncData.setMachineType( sfc.getMachineType() );
        ncData.setResrce( resource );
        ncData.setOperation( operation );
        ncData.setSfc( sfc.getSfc() );
        ncData.setItemBo( sfc.getItemBo() );
        ncData.setCarrier( carrier );
        ncData.setDateCode( dateCode );
        ncData.setNcCode( ncCode );
        ncData.setNcQty( ncQty );
        ncData.setDisposition( dispositionEnum.getValue() );
        if( dispositionEnum == DispositionEnum.REWORK ){
            ncData.setIsRework( "true" );
        }
        ncData.setCreatedDateTime( date );
        ncData.setCreateUserBo( CommonMethods.getUserBo() );
        ncData.setHandle( BOHandleUtil.buildNCDataHandle() );
        ncDataMapper.insertNcData( ncData );
    }

    /**
     * 查询本体自绑定起打开的不良记录
     *
     * @param site
     * @param carrier
     * @return
     */
    public List<NcData> findOpenNcDataFromDate( String site, String carrier ){

        List<CarrierBind> carrierBindList = carrierBindAdviceService.findCarrierBindOnBind( site, carrier );
        if( carrierBindList == null || carrierBindList.size() == 0 ){
            return new ArrayList<>();
        }
        Date fromDate = carrierBindList.get( 0 ).getStartDateTime();
        return  findOpenNcDataFromDate( site, carrier, fromDate );
    }

    /**
     * 查询本体在指定时间之后打开的不良记录
     *
     * @param site
     * @param carrier
     * @param fromDate
     * @return
     */
    public List<NcData> findOpenNcDataFromDate( String site, String carrier, Date fromDate ){
        /**检查站点*/
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ));
        /**检查本体编号*/
        Assert.hasText( carrier, I18nUtil.getI18nText( "common.carrierNotBlank" ));
        /**检查开始日期*/
        Assert.notNull( fromDate, I18nUtil.getI18nText( "common.dateNotNull" ));

        List<NcData> ncDataList = ncDataAdviceMapper.selectOpenNcDataFromDate( site, carrier, fromDate );
        return  ncDataList;
    }

    /**
     * 查询本体在指定时间和指定工序打开的不良记录
     *
     * @param site
     * @param carrier
     * @param fromDate
     * @param operation
     * @return
     */
    public List<NcData> findOpenNcDataFromDate( String site, String carrier, Date fromDate, String operation ){
        /**检查工序*/
        Assert.hasText( operation, I18nUtil.getI18nText( "common.operationNotBlank" ));

        List<NcData> _ncDataList = new ArrayList<NcData>();

        List<NcData> ncDataList = findOpenNcDataFromDate( site, carrier, fromDate );
        if( ncDataList == null || ncDataList.size() == 0 ){
            return _ncDataList;
        }

        for( NcData ncData : ncDataList ){
            if( ncData.getOperation().equals( operation ) ){
                _ncDataList.add( ncData );
            }
        }
        return _ncDataList;
    }

    /**
     * 查询本体自绑定起在指定工序打开的不良记录
     *
     * @param site
     * @param carrier
     * @param operation
     * @return
     */
    public List<NcData> findOpenNcDataFromDate( String site, String carrier, String operation ){
        /**检查工序*/
        Assert.hasText( operation, I18nUtil.getI18nText( "common.operationNotBlank" ));

        List<NcData> _ncDataList = new ArrayList<NcData>();

        List<NcData> ncDataList = findOpenNcDataFromDate( site, carrier );
        if( ncDataList != null ){
            for( NcData ncData : ncDataList ){
                if( ncData.getOperation().equals( operation ) ){
                    _ncDataList.add( ncData );
                }
            }
        }
        return _ncDataList;
    }

    /**
     * 查询本体在工序的不良信息
     *
     * @param site
     * @param carrier
     * @param itemBo
     * @param operation
     * @return
     */
    public NcData findCarrierNcDataFromDate( String site, String carrier, String itemBo, String operation ){
        /**检查工序*/
        Assert.hasText( operation, I18nUtil.getI18nText( "common.operationNotBlank" ));

        CarrierBind carrierBind = carrierBindAdviceService.findCarrierBindOnBindByItem( site, carrier, itemBo );
        NcData ncData = ncDataAdviceMapper.selectCarrierNcDataFromDate( site, carrier, itemBo, operation, carrierBind.getStartDateTime() );

        return ncData;
    }

    /**
     * 查询本体物料在工序打开不良项目
     *
     * @param site
     * @param carrier
     * @param itemBo
     * @param operation
     * @return
     */
    public NcData findItemOpenNcDataFromDate( String site, String carrier, String itemBo, String operation ){
        /**检查物料*/
        Assert.hasText( itemBo, I18nUtil.getI18nText( "common.operationNotBlank" ));

        List<NcData> ncDataList = findOpenNcDataFromDate( site, carrier, operation );
        if( ncDataList != null  ){
            for( NcData nd : ncDataList ){
                if( nd.getItemBo().equals( itemBo ) ){
                    return nd;
                }
            }
        }
        return null;
    }

    /**
     * 查询本体物料打开的不良信息
     *
     * @param site
     * @param carrier
     * @param itemBo
     * @return
     */
    public NcData findItemOpenNcDataFromDate( String site, String carrier, String itemBo ){
        CarrierBind carrierBind = carrierBindAdviceService.findCarrierBindOnBindByItem( site, carrier, itemBo );
        NcData ncData = ncDataAdviceMapper.selectItemOpenNcDataFromDate( site, carrier, itemBo, carrierBind.getStartDateTime() );

        return ncData;
    }
}
